package com.qr.collec;

import java.util.ArrayList;
import java.util.Iterator;

public class CollecMethod {

    public static void main(String[] args) {

        iteraTorColl();

    }


    /**
     * 遍历集合的方式
     * 所有实现coll的方法都可以使用迭代器来进行遍历
     * iterator仅用于遍历集合，并不用于存放对象
     */
    public static void iteraTorColl(){

        ArrayList<Book> books = new ArrayList<>();
        books.add(new Book("天龙八部","金庸",66.6));
        books.add(new Book("神雕","金庸",66.6));

        System.out.println(books);

        /**
         * 遍历
         */
        Iterator<Book> iterator = books.iterator();
     /*   while (iterator.hasNext()){
            Book next = iterator.next();
//            每个对象输出的格式     Book{name='神雕', author='金庸', price=66.6}
            System.out.println(next);
        }*/

        //快速的生成while循环 打    itit    就可以
        while (iterator.hasNext()) {
            Book next =  iterator.next();

        }
        //没有元素
        //  iterator.next();java.util.NoSuchElementException

        /**
         * 如果还要进行再次遍历
         * 就需要重置迭代器
         */
        iterator = books.iterator();
        while (iterator.hasNext()) {
            Book next = iterator.next();
            System.out.println(next);
        }

        /**
         * 增强for,简化版的迭代器
         *      这个增强for也是可以使用在数组上面的
         */
        for(Object book:books){
            System.out.println("增强for使用");
            System.out.println(book);
        }

//        int[] ints = new int[2];
//        ints[0] = 1;
//        ints[1] = 2;
        int[] ints = {1,2};
        for (Integer integer:ints){
            System.out.println(integer);
        }

        //增强for的快捷键，输入一个大写的I
        for (Object o :ints) {
            System.out.println("快捷键输入一个大写的I");
        }


    }


    public static void listArray(){

        ArrayList<Object> objects = new ArrayList<>();
        //泛型指定为object，则可以存放任何类型的元素
        objects.add("a");
        objects.add("a");
        objects.add("a");
        objects.add("a");
        objects.add("a");
        objects.add(true);
        objects.add(1);
        objects.add(1.1);

        System.out.println(objects);
        //移除第一个
        objects.remove("a");
        System.out.println(objects);

        //是为包含某一个元素
        System.out.println(objects.contains(true));

        //集合是否为空
        System.out.println(objects.isEmpty());

        //清空集合
        objects.clear();
        System.out.println(objects);


        ArrayList list = new ArrayList<>();
        list.add("111");
        list.add(2);
        //该方法可放入一个集合，将一个集合中的元素进行添加到我这个集合中
        boolean b = objects.addAll(list);
        System.out.println(b);
        System.out.println(objects);

        //查找多个元素是否存在,放入一个集合进行判断
        System.out.println(objects.containsAll(list));

        //删除多个元素
        objects.removeAll(list);
        System.out.println(objects);

    }

    public static class Book{

        private String name;
        private String author;
        private double price;

        public Book(String name, String author, double price) {
            this.name = name;
            this.author = author;
            this.price = price;
        }



        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getAuthor() {
            return author;
        }

        public void setAuthor(String author) {
            this.author = author;
        }

        public double getPrice() {
            return price;
        }

        public void setPrice(double price) {
            this.price = price;
        }

        @Override
        public String toString() {
            return "Book{" +
                    "name='" + name + '\'' +
                    ", author='" + author + '\'' +
                    ", price=" + price +
                    '}';
        }
    }

}
